package sf.database.template.enjoy.ext;

import com.jfinal.template.Directive;
import com.jfinal.template.Env;
import com.jfinal.template.expr.ast.Expr;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

/**
 * 使用 #page("...")
 */
public class PageDirective extends Directive {
    @Override
    public void exec(Env env, Scope scope, Writer writer) {
        Boolean isCount = (Boolean) scope.get(SqlKit.SQL_COUNT_KEY);
        if (isCount != null && isCount) {
            write(writer, "count(*)");
        } else {
            if (exprList.length() == 1) {
                Expr expr = exprList.getExpr(0);
                Object o = expr.eval(scope);
                if (o instanceof String) {
                    String str = (String) o;
                    write(writer, str);
                }
            }
        }
    }
}
